/************************************************************************/
/*Copyright (c) James Zhao 2007,2008
/*This file is part of AdminOverlay.
/*AdminOverlay is free software: you can redistribute it and/or modify
/*it under the terms of the GNU General Public License as published by
/*the Free Software Foundation, either version 3 of the License, or
/*(at your option) any later version.
/*
/*AdminOverlay is distributed in the hope that it will be useful,
/*but WITHOUT ANY WARRANTY; without even the implied warranty of
/*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/*GNU General Public License for more details.
/*
/*You should have received a copy of the GNU General Public License
/*along with Foobar.  If not, see <http://www.gnu.org/licenses/>.                                                                     */
/*************************************************************************/
#pragma once

#include <QtCore/QtCore>
#include "GraphicsKit.h"
#include "IRawInputListener.h"
#include "dllutils_ui.h"

class UIManager;

class EXPORT_UIKIT  UIControl :  public QObject
{
	Q_OBJECT
public:
	friend class UIManager;
public:
	UIControl(int x = 0, int y = 0, int width = 0, int height = 0);
	virtual ~UIControl(void);
public:
	int X() const;
	void X(int val);
	int Y() const;
	void Y(int val);

	QPoint Position() const;
	void Position(QPoint val);
	int Height() const;
	void Height(int val);
	int Width() const;
	void Width(int val);
	bool IsVisible() const;
	void IsVisible(bool val);
	const UIControl * Parent() const;
	void Parent(UIControl * val);
	QRect AbsoluteBounds() const;
	const QList<UIControl*>* Children() const;
	QRect LocalBounds();
	QPointer<QObject> Tag() const;
	void Tag(QPointer<QObject> val);
	bool HasFocus() const;
	void HasFocus(bool val);
	virtual void NeedUpdate(bool val);
	bool NeedUpdate() const;
	QString Text() const { return text; }
	void Text(QString val) { text = val; }
	QPoint GlobalPos() const;
	void GlobalPos(QPoint val);
	void GlobalX(int x);
	int GlobalX();
	void GlobalY(int y);
	int GlobalY();
public:
	virtual void layout(QPoint offset);
	virtual void layoutSelf(QPoint offset);
	virtual void layoutChildren(QPoint offset);
	virtual void draw(LPDIRECT3DDEVICE9 device);
	void addControl(UIControl *control);
	void removeControl(UIControl *control);
public:	
	QPoint localToGlobal(QPoint pt);
	QPoint globalToLocal(QPoint pt);
signals:
	void keyDown(int keyCode);
	void keyUp(int keyCode);
	void keyPressed(int keyCode, bool isControlDown);
	void mouseMoved(QPoint point, bool isMouseDown, bool isPrimaryTarget);
	void mouseDown(QPoint point, bool isPrimaryTarget);
	void mouseUp(QPoint point, bool isPrimaryTarget);
	//TODO: implement those 2 functions
	void mouseEntered();
	void mouseLeft();
protected:
	//event processing
	virtual void processOnKeyDown(int keyCode);
	virtual void processOnKeyUp(int keyCode);
	virtual void processOnKeyPress(int keyCode, bool isCtrlDown);
	virtual void processOnMouseMove(QPoint point, bool isMouseDown);
	virtual void processOnMouseDown(QPoint point);
	virtual void processOnMouseUp(QPoint point);
	//other events
	virtual void onDeviceReset();
protected:
	void AbsoluteBounds(QRect val);
	virtual void onBoundsChanged();
protected:
	QPoint position;
	int width, height;
	bool needUpdate;
	QPoint globalPos;
	bool hasBoundsChanged;
	QRect absoluteBounds;
	QList<UIControl*> children;
	UIControl *parent;
	QPointer<QObject> tag;
	bool hasFocus;
	QString text;
private:
	bool isVisible;
};
